#include "log.h"
#include "platform.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <rtc.h>


#define	MAX_LOG_LEN	100

os_mutex_t g_mutex = NULL;

static const char_t *log_level_str[] = {
	"ERR",
	"WARNING",
	"INFO",
	"DEBUG",
};

static void get_current_time(char_t *buf, int32_t len)
{
    T_TimeFmt tim;
    rtc_get_current_time(&tim);
	snprintf(buf, len - 1, "%04d%02d%02d %02d%02d%02d", (1900 + tim.year), (1 + tim.month), tim.date, tim.hour, tim.min, tim.sec);
}

void data_add_trace(char_t *msg);

//TODO: 增加写入文件
static void vlog_message(const int32_t level, const char_t *fmt, va_list args)
{
    char_t buf[MAX_LOG_LEN+1] = {0};
	char_t time[30] = {0};
    
    get_current_time(time, sizeof(time) - 1);
    sprintf(buf, "%s[%s]", time, log_level_str[level]);
	vsnprintf(&buf[strlen(buf)], sizeof(buf)-strlen(buf), fmt, args);
    printf(buf);
    
    //os_mutex_lock(&g_mutex);
    //data_add_trace(buf);
    //os_mutex_unlock(&g_mutex);
}

void log_message(const int32_t level, const char_t *fmt, ...)
{
	va_list args;
 
	va_start(args, fmt);
	vlog_message(level, fmt, args);
	va_end(args);
}

int32_t log_init(void){
    //g_mutex = os_mutex_create();
	return 0;
}
